var mRequest = require('../goodsDetailsService.js');
let app = getApp()
/**
 * 邀请好友帮忙砍价View
 */
Component({

  options: {
    // 在组件定义时的选项中启用多slot支持
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {
    isInvitationVisible:{
      type: Boolean,
      value: false
    }
  },

  /**
   * 私有数据,组件的初始数据
   * 可用于模版渲染
   */
  data: {
    goodsId: '',
    friendId: '',
    shareId: 6,                // 分享标识
    cutFlowerData: {},         // 砍花记录数据
    isBeInviterVisible: true,  // 被邀请者视角 样式    
    isInviterNothVisible: false,// 邀请者视角 默认样式  
    isInviterNumVisible: true, // 邀请者视角 有好友帮砍样式
    isShareVisible: false,     // ture 好友帮砍  false 立即邀请好友砍价
    isSeeMoreVisible: true,    // 砍花记录查看更多是否显示
    cutCurrentPage: 0,         // 砍花记录当前的页数
    ruleData:{},               //  砍花介绍
    goodsDetailsData: {}

  },

  methods: {
    initData: function () {
      // 获取dialog实例
      this.dialog = this.selectComponent("#dialog");
      // 获取砍果冻记录
      this.showCutFlower()
      // 获取规则信息
      this.showRule()
     
    },
    
    setGoodsDetailsData: function (data) {
      this.setData({
        goodsDetailsData: data
      })
    },

    /**
     * 获取规则信息
     */
    showRule: function () {
      let that = this
      let param = {
        type: 1
      }
      mRequest.getRule(param,
        function (data) {
          that.setData({
            ruleData: data
          })
        })

    },

    /**
     * 设置砍果冻的朋友id
     */
    initFirendId: function (id, goodId) {
      this.setData({
        friendId: id,
        goodsId: goodId
      })
    },

    /**
      * 获取砍果冻记录
      */
    showCutFlower: function () {
      var that = this
      var param = {
        startPage: this.data.cutCurrentPage,
        goodsId: this.data.goodsId,
        friendId: this.data.friendId
      }

      mRequest.getCutFlowerData(param,
        function (data) {
          if (that.data.cutCurrentPage == 0) {
            that.setData({
              cutFlowerData: data,
            })
            console.log('砍花记录  成功')
            // 0：自己，1：好友，2：陌生人
            if (data.type == 0) {
              if (data.goodCutRecordQueriesCount > 0) {
                that.setData({
                  isInviterNothVisible: true,    // 邀请者默认样式隐藏
                  isInviterNumVisible: false,    // 邀请者砍花显示
                  isShareVisible: false,         // 立即邀请好友砍花显示
                  isSeeMoreVisible: false        // 是否显示查看更多
                })
              }
            } else if (data.type == 1) {
              if (data.goodCutRecordQueriesCount > 0) {
                that.data.isSeeMoreVisible = false
              }
              that.setData({
                isBeInviterVisible: false,     // 好友视角显示
                isInviterNothVisible: true,    // 邀请者默认样式隐藏
                isInviterNumVisible: true,     // 邀请者砍花隐藏
                isShareVisible: true,          // 帮他砍花显示
                isSeeMoreVisible: that.data.isSeeMoreVisible // 是否显示查看更多
              })
            }
          } else {
            if (data.goodCutRecordQueries.length > 0) {
              let allData = that.data.cutFlowerData.goodCutRecordQueries.concat(data.goodCutRecordQueries)
              that.data.cutFlowerData.goodCutRecordQueries = allData
              that.setData({
                cutFlowerData: that.data.cutFlowerData
              })
            } else {
              that.showTost('没有更多数据');
              let page = that.data.cutCurrentPage - 1
              that.setData({
                isSeeMoreVisible: true,
                cutCurrentPage: page
              })
            }
          }
        })
    },

    /**
    * 砍花记录点击查看更多
    */
    cutFlowerMoreClick: function () {
      let page = this.data.cutCurrentPage + 1
      this.setData({
        cutCurrentPage: page
      })
      this.showCutFlower()
    },
    // 获取表单id
    sunmitForm:function(e){
      let formId = e.detail.formId;
      getApp().saveUserFormId(formId, 5);
      console.log("formId-5", formId)
    },
    
    /**
    * 帮他砍花
    */
    cutMoneyClick: function (e) {
      var that = this
      var param = {
        friendId: this.data.friendId,
        goodsId: this.data.goodsId,
      }
      mRequest.cutMoney(param,
        function (data) {
          if (data.code == -1) {
            that.showTost(data.message)
          } else {
            that.showTost('帮好友砍价成功')
            // 获取砍花记录
            that.showCutFlower()
          }
        })
    },

    /**
    * 点击砍花记录人物头像
    */
    clickReceiverItem: function (data) {
      wx.navigateTo({
        url: '/mineSubs/pages/homepage/personalhomepage?userId=' + data.currentTarget.dataset.itemdata.userId
      })
    },

    /**
     * 看家规则点击事件
     */
    ruleClick: function () {
      this.dialog.show()
    },

    /**
     * 海报分享
     */
    shareClick: function (e) {
      if (!this.data.isShareVisible) {
        // 邀请好友砍价分享
        return
      }
      let data = this.data.goodsDetailsData;
      wx.navigateTo({
        url: '/pages/goodsShare/goodsShare?good=' + encodeURIComponent(JSON.stringify(this.data.goodsDetailsData)) + '&pageType=' + 0,
      });
    },

    showTost: function (str) {
      wx.showToast({
        title: str,
        icon: 'none',
        duration: 1500,
        mask: true
      })
    },

  }
})
